package cn.edu.jnu.x2022152412;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = findViewById(R.id.edittext_frame_scores);
        Button button = findViewById(R.id.button_calculate);
        TextView textView = findViewById(R.id.textview_score);

        button.setOnClickListener(v -> {
            String input = editText.getText().toString().trim();
            if (input.isEmpty()) {
                textView.setText("请输入分数");
                return;
            }

            try {
                String normalizedInput = input.replace("，", ",");
                String[] parts = normalizedInput.split(",");

                BowlingGame game = new BowlingGame();
                for (String part : parts) {
                    part = part.trim();
                    if (!part.isEmpty()) {
                        int pins = Integer.parseInt(part);
                        if (pins < 0 || pins > 10) {
                            textView.setText("分数必须0-10");
                            return;
                        }
                        game.roll(pins);
                    }
                }

                textView.setText("总分: " + game.score());

            } catch (NumberFormatException e) {
                textView.setText("请输入有效的数字");
            } catch (Exception e) {
                textView.setText("错误: " + e.getMessage());
            }
        });
    }
}